package com.example.gameandroid;

import java.util.ArrayList;
import java.util.Random;

import org.andengine.engine.Engine;
import org.andengine.engine.camera.ZoomCamera;
import org.andengine.engine.handler.IUpdateHandler;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.shape.IAreaShape;
import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;

import android.hardware.SensorManager;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;

public class GameScene extends Scene {
	
	private PhysicsWorld physicsWorld;
	private Body body;
	private IAreaShape ball;
	private GameHUD gameHUD;
	private int marco;
	private int posMapa;
	private boolean controlaMapas;
	private Mapa m1;
	private Mapa m2;
	private Player player;
	private Sprite rock;
	private Engine engine;
	private ITiledTextureRegion botaoJumpTexture;
	private ITiledTextureRegion botaoRestartTexture;
	private float width;
	private float height;
	private ITextureRegion mapaTexture;
	private Font font;
	private ZoomCamera camera;
	private ITextureRegion rockTexture;
	private ITextureRegion gameOverTelaTexture;
	private ITiledTextureRegion playerTexture;
	private Random gerador;
	private Contact contact;
	private GameBD bd;
	private ArrayList<Rock> listRock;
	
	public GameScene(float height, float width, Engine engine, ITextureRegion mapaTexture, ITiledTextureRegion botaoJumpTexture, 
			ITiledTextureRegion playerTexture, ZoomCamera camera, Font font, ITextureRegion rockTexture, ITextureRegion gameOverTelaTexture, 
			ITiledTextureRegion botaoRestartTexture, GameBD bd){
		
		this.engine = engine;
		this.botaoJumpTexture = botaoJumpTexture;
		this.botaoRestartTexture = botaoRestartTexture;
		this.width = width;
		this.height = height;
		this.mapaTexture = mapaTexture;
		this.font = font;
		this.camera = camera;
		this.rockTexture = rockTexture;
		this.gameOverTelaTexture = gameOverTelaTexture;
		this.playerTexture = playerTexture; 
		
		gerador = new Random();
		listRock = new ArrayList<Rock>();
		
        physicsWorld = new PhysicsWorld(new Vector2(0,SensorManager.GRAVITY_EARTH), false);        
        
        m1 = new Mapa(engine, width, height, mapaTexture, physicsWorld);  
        m1.setPosition(0, 0);
        attachChild(m1); 
        
        m2 = new Mapa(engine, width, height, mapaTexture, physicsWorld);
        m2.setPosition(m1.getMapaWidth(), 0);  
        attachChild(m2);
        
        controlaMapas = false;
        marco = (int) (m1.getMapaWidth()*1.49f);
        posMapa = (int) (m2.getMapaWidth() * 2); 
        
        ball = new Rectangle(0, 0, width * 0.05f, width * 0.05f, engine.getVertexBufferObjectManager());
        
        ball.setPosition(0, height - ball.getHeight() - 100);
        ball.setColor(1, 1, 1);
        
        rock = new Sprite(-150, 0, rockTexture, engine.getVertexBufferObjectManager());
        attachChild(rock);
        
        FixtureDef fix = PhysicsFactory.createFixtureDef(0.2f, 0, 0.2f);
        body = PhysicsFactory.createCircleBody(physicsWorld, ball, BodyType.DynamicBody, fix);
        body.setUserData("player");
        physicsWorld.registerPhysicsConnector(new PhysicsConnector(ball, body, true, false));
        physicsWorld.setAutoClearForces(true);
        attachChild(ball); 
        ball.setVisible(false);        
        
        player = new Player(engine, playerTexture, width, physicsWorld, body);
        player.setScale(2.5f);
        player.setPosition(100, height - 400);
        attachChild(player);
        player.animaCorrer();
        
        
        contact = new Contact(body, player);
        physicsWorld.setContactListener(contact);
        
        camera.setChaseEntity(ball);
        
        registerUpdateHandler(physicsWorld);        
       
		gameHUD = new GameHUD(engine, physicsWorld, botaoJumpTexture, width, height, body, font, player);
		camera.setHUD(gameHUD);
       
        registerUpdateHandler(gameLoop());
        System.out.println("MARCO: " + marco); 
        System.out.println("kaaaao: " + (player.getSprite().getHeight() + "  " + ball.getHeight()));
	
	}
	
	public IUpdateHandler gameLoop(){
		IUpdateHandler loop = new IUpdateHandler() {
			@Override
			public void reset() {
			}
		
			@Override
			public void onUpdate(float pSecondsElapsed) {  
				
				if(player.getSprite().collidesWith(rock)){
					gameOver();
					System.out.println("GAMEOVER");
				}
				player.setPosition(ball.getX()-250, ball.getY() - 58);
				
				if(player.getBody().getLinearVelocity().x < 10){
					player.correr();
				}

				if(Math.round(ball.getX()) > marco && Math.round(ball.getX()) < marco + 30 ){
					if(controlaMapas){ 
						m2.setPosition(posMapa, m2.getY()); 
						int n = gerador.nextInt(3);
						System.out.println("Random: " + n);
						listRock.add(new Rock(engine, rockTexture));
						rock.setPosition(posMapa + 150 * n, height - (rock.getHeight() * 2));
						controlaMapas = false;
					}else{
						m1.setPosition(posMapa, m1.getY()); 
						controlaMapas = true;
					}
					
					marco += m1.getMapaWidth();
					posMapa += m1.getMapaWidth();
				}					
				gameHUD.setPontos(Math.round(ball.getX()/10));
			}
		};
		return loop;		
	}
	
	public void gameOver(){
		engine.setScene(new GameOverScene(engine, botaoJumpTexture, gameOverTelaTexture, width, height, 
				mapaTexture, font, camera, bd, rockTexture, playerTexture, botaoRestartTexture));
	}
	
	public void createPhysicsWorld(){
		 physicsWorld = new PhysicsWorld(new Vector2(0,SensorManager.GRAVITY_EARTH), false);
	}
}
